package com.netty.config;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lzn
 * @date 2/23/21 4:57 PM
 */
public class NettyConfig {
    public static Map<String, ChannelGroup> moduleChannelGroup = new HashMap<>();
    // 存储每一个客户端接进来的对象
    public static ChannelGroup group = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    // 添加分组webSocket对象
    public static void AddModuleChannelGroup(ChannelHandlerContext ctx,String module){
        if(moduleChannelGroup.containsKey(module)){
            moduleChannelGroup.get(module).add(ctx.channel());
        }else{
            ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
            channelGroup.add(ctx.channel());
            moduleChannelGroup.put(module,channelGroup);
        }
    }
    public static void removeModuleChannelGroup(ChannelHandlerContext ctx){
       for(Map.Entry<String,ChannelGroup> entry : moduleChannelGroup.entrySet()){
           if(entry.getValue() == null){
               moduleChannelGroup.remove(entry.getKey());
               break;
           }

       }
    }
}
